package com.ym.controller.doctor.contacts;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.exception.BusinessException;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.dao.AddressGroupDao;
import com.ym.medical.domain.entity.AddressGroupEntity;
import com.ym.medical.domain.entity.AddressListEntity;
import com.ym.medical.domain.entity.PatientDetailEntity;
import com.ym.medical.domain.entity.PatientFollowupEntity;
import com.ym.medical.domain.entity.PatientHealthRecordsEntity;
import com.ym.medical.domain.param.PatientHealthRecordsParam;
import com.ym.medical.domain.vo.MyPatientVO;
import com.ym.medical.enums.AddressListIdTypeEnum;
import com.ym.medical.enums.PatientFollowupStateEnum;
import com.ym.medical.enums.SystemGroupEnum;
import com.ym.medical.service.IAddressGroupSV;
import com.ym.medical.service.IAddressListSV;
import com.ym.medical.service.IInquiryOrderSV;
import com.ym.medical.service.IPatientDetailSV;
import com.ym.medical.service.IPatientFollowupSV;
import com.ym.medical.service.IPatientHealthRecordsSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通讯录患者相关接口
 *
 * @author: 李涛
 * @version: 2019年05月22日 10:23
 */
@Api(tags = "医生端-通讯录-患者相关")
@RestController
@ApiSign
@RequestMapping(value = "/doctor/contacts/patient", headers = Constants.Api.VERSION)
@Validated
public class ContactsPatientController extends BaseController {

	@Autowired
	private IPatientHealthRecordsSV patientHealthRecordsSV;

	@Autowired
	private IPatientFollowupSV iPatientFollowupSV;

	@Autowired
	private IAddressGroupSV iAddressGroupSV;

	@Autowired
	private IPatientDetailSV iPatientDetailSV;

	@Autowired
	private IAddressListSV iAddressListSV;

	@Autowired
	private AddressGroupDao addressGroupDao;

	@Autowired
	private IInquiryOrderSV inquiryOrderSV;


	@Log(dataBase = false, printBody = false)
	@ApiOperation(value = "患者健康档案详情查询", notes = "两个参数至少填写一个")
	@GetMapping("/archives/info")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "patientId", value = "患者ID", required = true, paramType = "query"),
			@ApiImplicitParam(name = "archivesId", value = "档案ID", required = true, paramType = "query"),
	})
	public APIResponse<List<PatientHealthRecordsEntity>> orders(
			Long patientId,
			Long archivesId) {
		if (patientId == null && archivesId == null) {
			throw new BusinessException("请至少传递一个参数");
		}
		PatientHealthRecordsParam patientHealthRecords = new PatientHealthRecordsParam();
		patientHealthRecords.setPatientId(patientId);
		patientHealthRecords.setId(archivesId);
		List<PatientHealthRecordsEntity> patientHealthRecords1 = patientHealthRecordsSV.selectList(patientHealthRecords.covert());
		return successData(patientHealthRecords1);
	}

	@Log
	@ApiOperation(value = "患者随访新增", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@PostMapping(value = "/follow/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "patientIds", value = "患者ID", required = true, paramType = "form"),
			@ApiImplicitParam(name = "content", value = "随访内容", required = true, paramType = "form"),
	})
	public APIResponse orders(
			@NotNull Long[] patientIds,
			@NotEmpty String content
	) {
		for (Long patientId : patientIds) {
			if (patientId == null) {
				continue;
			}
			PatientFollowupEntity patientFollowup = new PatientFollowupEntity();
			patientFollowup.setDoctorId(ShiroUtil.getLoginId());
			patientFollowup.setFollowContent(content);
			patientFollowup.setFollowState(PatientFollowupStateEnum.NO_RESPONSE.getKey());
			patientFollowup.setPatientId(patientId);
			iPatientFollowupSV.insert(patientFollowup);
		}
		return success();
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("分组好友列表")
	@GetMapping(value = "/getGroupFriends")
	@ResponseBody
	public APIResponse<PageInfo<MyPatientVO>> getGroupFriends(
			@ApiParam(name = "pageNum", value = "页码", required = false)
			@RequestParam(name = "pageNum", required = true, defaultValue = "1")
					int pageNum,
			@ApiParam(name = "pageSize", value = "每页数量", required = false)
			@RequestParam(name = "pageSize", required = false, defaultValue = "10")
					int pageSize,
			@ApiParam(name = "groupId", value = "分组id", required = true)
			@RequestParam Long groupId,
			@ApiParam(name = "type", value = "01 我的粉丝 02 其他组", required = true)
			@RequestParam String type,
			@ApiParam(name = "keyword", value = "关键字")
			@RequestParam(required = false) String keyword) throws Exception {
		Long userId = ShiroUtil.getLoginId();
		PageInfo<MyPatientVO> groupFriends = null;
		if ("01".equals(type)) {//粉丝
			groupFriends =
					iAddressGroupSV.getMyGroupFan(pageNum, pageSize, groupId, userId, keyword);

		} else {//患者
			groupFriends =
					iAddressGroupSV.getGroupFriends(pageNum, pageSize, groupId, userId, keyword);
		}

		return successData(groupFriends);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("患者基本信息")
	@GetMapping(value = "/getPatientInfo")
	@ResponseBody
	public APIResponse<Map<String, Object>> getPatientInfo(
			@ApiParam(name = "patientId", value = "患者id", required = true)
			@RequestParam Long patientId,
			@ApiParam(name = "idType", value = "标志01 或 02 该值已经在列表中返回直接取值", required = true)
			@RequestParam String idType) throws Exception {
		Long currUser = ShiroUtil.getLoginId();
		Map<String, Object> map = new HashMap<>();
		//查询是否是特别关注用户
		AddressGroupEntity addressGroup = iAddressGroupSV.getAttentionPatient(currUser, patientId);
		PatientDetailEntity patientDetail = null;
		if ("01".equals(idType)) {//用户id
			patientDetail = iPatientDetailSV.selectByUserId(patientId);

		} else {//患者id
			patientDetail = iPatientDetailSV.selectBypatientId(patientId);
		}
		map.put("askNum", inquiryOrderSV.getPatientAskNum(currUser, patientDetail.getId()));
		map.put("getAttention", addressGroup);
		map.put("patientDetail", patientDetail);
		return successData(map);

	}

	@Log
	@ApiOperation("患者分组")
	@PostMapping(value = "/groupingPatient", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@ResponseBody
	public APIResponse groupingPatient(
			@ApiParam(name = "patientId", value = "患者id", required = true)
					Long patientId,
			@ApiParam(name = "groupId", value = "分组id", required = true)
					Long groupId,
			@ApiParam(name = "idType", value = "标志01 或 02 该值已经在列表中返回直接取值", required = true)
					String idType) throws Exception {
		AddressListEntity addressList = new AddressListEntity();
		addressList.setAlgId(groupId);
		addressList.setUserId(patientId);
		addressList.setIdType(idType);
		iAddressListSV.insert(addressList);
		return successMsg("操作成功");
	}

	@Log
	@ApiOperation("取消(关注)特别关注")
	@GetMapping(value = "/cancelAttention")
	@ResponseBody
	public APIResponse<AddressListEntity> cancelAttention(
			@ApiParam(name = "patientId", value = "患者id", required = true)
					Long patientId,
			@ApiParam(name = "groupId", value = "分组id", required = true)
					Long groupId,
			@ApiParam(name = "type", value = "01 关注 02 取消关注", required = true)
					String type,
			@ApiParam(name = "idType", value = "01 粉丝 02 其他", required = true)
					String idType) throws Exception {
		long currUser = ShiroUtil.getLoginId();
		long algId = 0;
		List<AddressGroupEntity> list = addressGroupDao.selectByDoctId(currUser);
		for (AddressGroupEntity a : list) {
			if (SystemGroupEnum.PATIENT.ATTENTION.getName().equals(a.getAlgName())) {
				algId = a.getId();
			}
		}
		int rows = 0;
		AddressListEntity model = null;
		if ("01".equals(type) && "01".equals(idType)) {
			model = AddressListEntity.builder().algId(algId)
					.userId(patientId).idType(AddressListIdTypeEnum.THE_USER.getKey()).build();
			iAddressListSV.insert(model);
			return successData(model);
		} else if ("01".equals(type) && "02".equals(idType)) {
			model = AddressListEntity.builder().algId(algId)
					.userId(patientId).idType(AddressListIdTypeEnum.IN_PATIENTS_WITH.getKey()).build();
			iAddressListSV.insert(model);
			return successData(model);
		} else {
			model = AddressListEntity.builder().algId(groupId)
					.userId(patientId).build();
			rows = iAddressListSV.deleteByParams(model);
			if (rows != 0) {

				return successMsg("取消成功");
			}
		}
		return successData(null);

	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("我的粉丝")
	@GetMapping(value = "/getFan")
	@ResponseBody
	public APIResponse<PageInfo<MyPatientVO>> getFan(
			@ApiParam(name = "pageNum", value = "页码", required = true)
			@RequestParam(name = "pageNum", required = true, defaultValue = "1")
					int pageNum,
			@ApiParam(name = "pageSize", value = "每页数量", required = false)
			@RequestParam(name = "pageSize", required = false, defaultValue = "10")
					int pageSize,
			@ApiParam(name = "groupId", value = "分组id", required = false)
			@RequestParam(name = "groupId", required = false)
					Long groupId,
			@ApiParam(name = "keyword", value = "关键字", required = false)
			@RequestParam(name = "keyword", required = false)
					String keyword) throws Exception {
		Long userId = ShiroUtil.getLoginId();
		PageInfo<MyPatientVO> groupFan =
				iAddressGroupSV.getFan(pageNum, pageSize, groupId, userId, keyword);
		return successData(groupFan);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("全部患者检索")
	@GetMapping(value = "/selectAll")
	@ResponseBody
	public APIResponse<PageInfo<MyPatientVO>> selectAll(
			@ApiParam(name = "pageNum", value = "页码", required = true)
			@RequestParam(name = "pageNum", required = true, defaultValue = "1")
					int pageNum,
			@ApiParam(name = "pageSize", value = "每页数量", required = false)
			@RequestParam(name = "pageSize", required = false, defaultValue = "10")
					int pageSize,
			@ApiParam(name = "keyword", value = "关键字", required = false)
			@RequestParam(name = "keyword", required = false)
					String keyword) throws Exception {
		Long userId = ShiroUtil.getLoginId();
		PageInfo<MyPatientVO> all =
				iAddressGroupSV.selectAll(pageNum, pageSize, userId, keyword);
		return successData(all);

	}
}
